import cv2
import numpy as np
if __name__ == '__main__':
    image_np = cv2.imread('work4.jpg')
    points = [[55, 25],[410, 120],[40, 668],[500, 590]]
    pts1 = np.float32(points)
    image_line = image_np.copy()
    cv2.line(image_line, points[0], points[1], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[1], points[3], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[3], points[2], (0, 0, 255), 2, cv2.LINE_AA)
    cv2.line(image_line, points[2], points[0], (0, 0, 255), 2, cv2.LINE_AA)

    img_shape = image_np.shape
    points = [[0, 0], [img_shape[1], 0], [0, img_shape[0]], [img_shape[1], img_shape[0]]]
    pts2 = np.float32(points)
    M = cv2.getPerspectiveTransform(pts1, pts2)
    correct_image = cv2.warpPerspective(image_np, M, (img_shape[1], img_shape[0]), cv2.INTER_LANCZOS4, cv2.BORDER_WRAP)


    # image_np_gray = cv2.cvtColor(correct_image, cv2.COLOR_BGR2GRAY)
    # ret, image_np_gray = cv2.threshold(image_np_gray, 127, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)

    image_hsv = cv2.cvtColor(correct_image, cv2.COLOR_BGR2HSV)
    image_mask_white = cv2.inRange(image_hsv, np.array([0, 0, 150]), np.array([180, 43, 255]))
    image_mask_dark = cv2.inRange(image_hsv, np.array([0, 0, 0]), np.array([180, 255, 80]))



    h, s, v = cv2.split(image_hsv)
    v = np.where(image_mask_white > 0, np.clip(v + 55, 180, 200), v)
    v = np.where(image_mask_dark > 0, np.clip(v-70, 70, 80 ), v)
    image_finall = cv2.merge((h, s, v))
    image_finall = cv2.cvtColor(image_finall, cv2.COLOR_HSV2BGR)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
    morph_image1 = cv2.erode(image_finall, kernel)
    morph_image2 = cv2.dilate(morph_image1, kernel)
    cv2.imshow('', morph_image2)
    # cv2.imshow('', image_finall)
    cv2.imwrite('zuoye4.jpg', morph_image2)
    cv2.waitKey(0)